package at.fhj.itm.weather;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.NoSuchElementException;

import android.os.AsyncTask;
import android.util.Log;

public class WeatherDataYahoo extends AsyncTask<String, Integer, WeatherData> {

	private WeatherDataLoader weatherDataListener;

	
	public WeatherDataLoader getWeatherDataListener() {
		return weatherDataListener;
	}

	public void setWeatherDataListener(WeatherDataLoader weatherDataListener) {
		this.weatherDataListener = weatherDataListener;
	}

	

	private static final String APPID = "D2cJ3PTV34F.UXhIWlOHnmZJ2zMe6HiuW7WRy0uXZtnLC2bUKSsKI5DaLb0S_emImsUVz._Jz3Z._sv1bw--)";

	private static String getFromURL(String urlStr) {
		Log.i("WeatherApp", "getFromURL"+urlStr);
		StringBuilder sb = new StringBuilder();
		InputStream in = null;
		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection) new URL(urlStr.replaceAll(" ", "%20")).openConnection();
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				in = conn.getInputStream();
				int bufChar;
				while ((bufChar = in.read()) != -1) {
					sb.append(String.valueOf((char) bufChar));
				}
				return sb.toString();
			}
			throw new NoSuchElementException();
		} catch (IOException ioe) {
			throw new NoSuchElementException();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
				}
			if (conn != null)
				conn.disconnect();
		}
	}

	@Override
	protected WeatherData doInBackground(String... arg0) {
		//resolve address to woeid (yahoos where on earth id)
		String woeid = null;
		try {
			String data = getFromURL("http://where.yahooapis.com/v1/places.q("
					+ arg0[0] + ")?appid=" + APPID);
			int woeidstart = data.indexOf("<woeid>") + "<woeid>".length();
			int woeidend = data.indexOf("</woeid>", woeidstart);
			if(woeidstart>0&&woeidend>woeidstart)
				woeid = data.substring(woeidstart, woeidend);
			return new WeatherData("http://weather.yahooapis.com/forecastrss?w=" +
					 woeid + "&u=c&appid=" + APPID);
		} catch (NoSuchElementException e) {
			Log.i("WeatherApp", "getFromURL NoSuchElementException" + e.getMessage());
			return null;
		} catch (MalformedURLException e) {
			Log.i("WeatherApp", "WeatherData MalformedURLException" + e.getMessage());
			return null;
		}
		
	}
	
	public static String getPlaceFromGeo(double lat, double lng)
	{
		//Yahoo placefinder
		//http://where.yahooapis.com/geocode?q=38.898717,+-77.035974&gflags=R&appid=[yourappidhere]
		String city = "not found";
		try {
			String data = getFromURL("http://where.yahooapis.com/geocode?q="+lng+"+"+lat+"&gflags=R&appid=" + APPID);
			int citystart = data.indexOf("<city>") + "<city>".length();
			int cityend = data.indexOf("</city>", citystart);
			if(citystart>0&&cityend>citystart)
				city = data.substring(citystart, cityend);
		} catch (NoSuchElementException e) {
			Log.i("WeatherApp", "getFromURL NoSuchElementException" + e.getMessage());
		}
		return city;
		
	}

	@Override
	protected void onPostExecute(WeatherData result) {
		weatherDataListener.update(result);
	}

}
